﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;


namespace QuestPrototype.Screen_Manager.Screens
{
    class Debug : BaseScreen
    {


        public string Screens = "";
        public string FocusScreen = "";

        private int fps = 0;
        private int fpsCounter;
        private double fpsTimer;
        private string fpsText = "";

        private Rectangle BGRect;

        private bool KeyDown = false;

        public Debug()
        {
            Name = "Debug";
            State = ScreenState.Hidden;
            GrabFocus = false;
        }

        public override void HandleInput()
        {

            if (Keyboard.GetState().IsKeyDown(Keys.F1) && KeyDown == false)
            {
                KeyDown = true;
                if (State == ScreenState.Active)
                {
                    State = ScreenState.Hidden;
                }
                else if (State == ScreenState.Hidden)
                {
                    State = ScreenState.Active;
                }
            }
            if (Keyboard.GetState().IsKeyUp(Keys.F1) && KeyDown == true)
            {
                KeyDown = false;
            }

        }

        public override void Update()
        {
             base.Update();

            if (Screens.Length > 0)
	{
		  Screens = Screens.Substring(0, Screens.Length - 2);
	}

        int  txtWidth = 0;
        int txtHeight = 0;

            if (Fonts.Arial_8.MeasureString(Screens).X >txtWidth ) 
	{
		txtWidth = (int)Fonts.Arial_8.MeasureString(Screens).X; 
	}
            if (Fonts.Arial_8.MeasureString(FocusScreen).X > txtWidth)
	{
        txtWidth = (int)Fonts.Arial_8.MeasureString(FocusScreen).X;
	}
            txtHeight = (int)Fonts.Arial_8.MeasureString(fpsText).Y * 3;
        BGRect =  new Rectangle(0, 0, txtWidth + 20, txtHeight + 20);

        }

        public override void Draw()
        {
            base.Draw();

            if (Globals.GameTime.TotalGameTime.TotalMilliseconds > fpsTimer)
            {
                fps = fpsCounter;
                fpsTimer = Globals.GameTime.TotalGameTime.TotalMilliseconds + 1000;
                fpsCounter = 1;
                fpsText = "FPS: " + fps;
            }
            else
            {
                fpsCounter += 1;
            }


            Globals.SpriteBatch.Begin();
            Globals.SpriteBatch.Draw(Textures.RadAvatar, BGRect, Color.Black * 0.6F);
            Globals.SpriteBatch.DrawString(Fonts.Arial_8, fpsText, new Vector2(10, 10), Color.White);
            Globals.SpriteBatch.DrawString(Fonts.Arial_8, Screens, new Vector2(10, 22), Color.White);
            Globals.SpriteBatch.DrawString(Fonts.Arial_8, FocusScreen, new Vector2(10, 34), Color.White);
            Globals.SpriteBatch.End();
        }
    }
}
